var ordinateTransf = require('./ordinateTransf.js');

const imgPre = '';

const imgHander = '';

const imgHanderDetail = '';

const base_url_html = '';
//配套的高德小程序的key
const amapPlugin_key_wx = '';


const versionNumber = '1.1.4';

const checkStrLen = (str) => {
	let isok = false;
	if (str.length <= 15) {
		isok = true;
	}
	return isok;
}
const checkStrLenByNum = (str, num = 5) => {
	let isok = false;
	if (str.length <= num) {
		isok = true;
	}
	return isok;
}
//判断云端是否返回成功
const isResSuccess = (res) => {
	let isSuccess = false;
	if (res.data.flag === 1) {
		isSuccess = true;
	}
	return isSuccess;
}

//判断手机号是否正确
const checkPhone = (phone) => {
	let isok = false;
	if ((/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))) {
		isok = true;
	}
	return isok;
}

//判断验证码位数是否正确
const checkCode = (code) => {
	let isok = false;
	if ((/^\d{4}$/.test(code))) {
		isok = true;
	}
	return isok;
}


//生成GUID （全球唯一标识符）
const getGuid = () => {
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		var r = Math.random() * 16 | 0,
			v = c == 'x' ? r : (r & 0x3 | 0x8);
		return v.toString(16);
	});
}

//坐标转换
//WGS84：为一种大地坐标系，也是目前广泛使用的GPS全球卫星定位系统使用的坐标系。
//GCJ02：又称火星坐标系，是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系
//BD09：为百度坐标系，在GCJ02坐标系基础上再次加密。其中bd09ll表示百度经纬度坐标，bd09mc表示百度墨卡托米制坐标。
//h5在微信浏览器里、uniapp是使用 gcj02 国测局坐标系
//WGS84  GCJ02  BD09
// const changeAdress_bd_To_gcj = (adress) => {
//  	let ret = adress;

// 	let bdLat = ret.latitude ;
// 	let bdLon = ret.longitude ;

// 	let gcjLatLon = ordinateTransf.bd_decrypt_To_gcj(bdLat,bdLon);

// 	ret.longitude = gcjLatLon.lon;
// 	ret.latitude = gcjLatLon.lat;

// 	// #ifdef H5
// 	//H5苹果不展示，要parseFloat一下
// 	ret.longitude = parseFloat(gcjLatLon.lon);
// 	ret.latitude = parseFloat(gcjLatLon.lat);
// 	// #endif

// 	console.log('ret.latitude',ret.latitude);
// 	console.log('ret.longitude',ret.longitude);
//  	return ret;
//  }

const changeAdress_bd_To_gcj = (latitude, longitude) => {
	let ret = {};

	let bdLat = latitude;
	let bdLon = longitude;

	let gcjLatLon = ordinateTransf.bd_decrypt_To_gcj(bdLat, bdLon);

	ret = {
		latitude: parseFloat(gcjLatLon.lat),
		longitude: parseFloat(gcjLatLon.lon)
	}

	//console.log('ret.latitude',ret.latitude);
	//console.log('ret.longitude',ret.longitude);
	return ret;
}

const changeAdress_bd_To_wgs = (adress) => {
	let ret = adress;

	let bdLat = ret.latitude;
	let bdLon = ret.longitude;

	let gcjLatLon = ordinateTransf.bd_decrypt_To_gcj(bdLat, bdLon);
	let wgsLatLon = ordinateTransf.gcj_decrypt_To_wgs(gcjLatLon.lat, gcjLatLon.lon);

	ret.longitude = wgsLatLon.lon;
	ret.latitude = wgsLatLon.lat;
	//console.log('ret.latitude',ret.latitude);
	//console.log('ret.longitude',ret.longitude);
	return ret;
}


//登录成功的跳转
const toLogin_suc_toPage = () => {
	let delta = 1;
	//检查要跳的页面是不是登录页，是登录页，就跳首页
	let isLogin = false;
	let pages = getCurrentPages(); // 获取当前页面栈
	try {
		let curPage = pages[pages.length - delta - 1];

		if (curPage.route == 'pages/user/login') {
			isLogin = true;
		}

	} catch (e) {
		isLogin = true; //取页面栈报错，去首页
		//TODO handle the exception
	}

	if (isLogin) {
		let toPage = setTimeout(function() {
			uni.switchTab({
				url: `/pages/index/index`,
			})
			clearTimeout(toPage);
		}, 800)
	} else {
		let toPage = setTimeout(function() {
			uni.navigateBack({
				delta: delta,
			})
			clearTimeout(toPage);
		}, 800)
	}


}
const toLoginPage = () => {
	uni.navigateTo({
		url: `/pages/user/login`
	})
}

module.exports = {
	imgPre: imgPre,
	imgHander: imgHander,
	imgHanderDetail: imgHanderDetail,
	base_url_html: base_url_html,
	versionNumber: versionNumber,
	isResSuccess: isResSuccess,
	checkPhone: checkPhone,
	checkCode: checkCode,
	changeAdress_bd_To_gcj: changeAdress_bd_To_gcj,
	changeAdress_bd_To_wgs: changeAdress_bd_To_wgs,
	checkStrLen,
	amapPlugin_key_wx,
	checkStrLenByNum,
	
	getGuid,
	toLoginPage,
}
